new Vue({
	el: "body",
	data: {
		loading:false,
		shopdata: {},
		shopid: "",
		shopname: "",
		shopinfopage: false,
		showcomment: false,
		shophours: {},
		shopimg: "",
		star: [],
		productclassify: [],
		classify: [],
		bgc: 0,
		total: '0',
		startprice: "",
		sendprice: "",
		count: "",
		productsdata: [],
		moremoney: ""
	},
	created: function() {
		var shopid = geturl_ch('shopid');
		this.leancloudInit();
		this.findshop(shopid);
	},
	methods: {
		leancloudInit: function() {
			var APP_ID = 'c4w6bVvWfh8xxcmkBE1G0gIt-gzGzoHsz';
			var APP_KEY = 'JBikg4Ksk76zcTi5BKYWvTJF';
			AV.init({
				appId: APP_ID,
				appKey: APP_KEY
			});
		},
		findshop: function(shopid) {
			//根据距离筛选商家
			var _this = this;
			var juli;
			var query = new AV.Query('ELM_shop');
			query.get(shopid).then(function(data) {
				// 成功获得实例
				_this.shopdata = data;
				_this.shopid = data.id;
				_this.shopname = data.attributes.shopname;
				_this.shopimg = data.attributes.shopimg;
				_this.shophours = JSON.parse(data.attributes.shophours);
				_this.star = parseInt(data.attributes.grade);
				_this.productclassify = eval(data.attributes.productclassify);
				_this.startprice = data.attributes.startprice;
				_this.sendprice = data.attributes.sendprice;
				_this.moremoney = data.attributes.startprice;
			}, function(error) {
				// 异常处理
			}).then(function() {
				var productquery = new AV.Query('ELM_product');
				productquery.contains('shopid', '' + shopid + '');
				productquery.find().then(function(data) {
					_this.classify = data;
					for(var i = 0; i < data.length; i++) {
						_this.geshu[data[i].id] = data[i].attributes.geshu;
					}
				}, function(error) {
					// 异常处理
				});
			}).then(function(){
				_this.loading=true;
			});
		},
		backpage: function() {
			history.back();
		},
		changeBgc: function(i) {
			this.bgc = i;
		},
		add: function(id, name, price) {
			if(this.count == "") {
				this.count = 1;
			} else {
				this.count = (this.count) + 1;
			}
			this.productsdata.push({
				"id": id,
				"name": name,
				"price": price
			});
			var total1 = this.total;
			this.total = (parseInt(total1) + parseInt(price)).toString();
			var startprice = this.startprice;
			var total2 = this.total;
			this.moremoney = (parseInt(startprice) - parseInt(total2)).toString();
		},
		pay: function() {
			var _this = this;
			var currentUser = AV.User.current();
			if(!currentUser) {
				var a = confirm("猴哥提醒您登陆后购买");
				if(a == true) {
					location.href = "../login/login.html";
				}
			} else {
				var nowtime = new Date().toLocaleString();
				var phonenum = AV.User.current().attributes.username;
				var username = "";
				var userdata = new AV.Query('ELM_user');
				userdata.contains('phonenum', phonenum);
				userdata.find().then(function(data) {
					username = data[0].attributes.username;
					var Product = AV.Object.extend('ELM_order');
					var product = new Product();
					product.set('if_pay', false);
					product.set('shopid', _this.shopid);
					product.set('shopimg', _this.shopimg);
					product.set('buy_time', nowtime);
					product.set('shop_sendprice', _this.sendprice);
					product.set('username', username);
					product.set('user_phonenum', phonenum);
					product.set('shop_name', _this.shopname);
					product.set('total', _this.count * 1);
					product.set('totle', _this.total);
					product.set('products_data', _this.productsdata);
					product.set('user_adress', data[0].attributes.adress);
					product.save().then(function(data) {
						//  发布成功
						location.href = "../order/order.html?orderid="+seturl(data.id);
					}, function(error) {
						alert("添加订单失败");
					});
				}, function(error) {
					// 异常处理
				});
			}
		}
	}
})